<template>
    <div class="app-container">
        <el-form
            :model="queryParams"
            ref="queryForm"
            size="small"
            :inline="true"
            v-show="showSearch"
            label-width="68px"
        >
            <el-form-item label="部门" prop="deptId">
                <treeselect
                    v-model="queryParams.deptId"
                    :options="deptOptions"
                    :show-count="true"
                    placeholder=""
                    @keyup.enter.native="handleQuery"
                    style="width: 215px"
                />
            </el-form-item>
            <el-form-item label="姓名" prop="realName">
                <el-input
                    v-model="queryParams.realName"
                    placeholder=""
                    clearable
                    @keyup.enter.native="handleQuery"
                />
            </el-form-item>
            <el-form-item label="日期" prop="yearMonth">
                <el-date-picker
                    v-model="queryParams.yearMonth"
                    value-format="yyyy-MM"
                    type="month"
                    placeholder="选择月"
                >
                </el-date-picker>
            </el-form-item>

            <el-form-item>
                <el-button
                    type="primary"
                    icon="el-icon-search"
                    size="mini"
                    @click="handleQuery"
                    >查询</el-button
                >
                <el-button
                    icon="el-icon-refresh"
                    size="mini"
                    @click="resetQuery"
                    >重置</el-button
                >
            </el-form-item>
        </el-form>

        <TableCard>
            <el-row :gutter="10" class="mb8">
                <el-col :span="1.5">
                    <el-button
                        type="primary"
                        plain
                        icon="el-icon-plus"
                        size="mini"
                        @click="handleAdd"
                        >新建</el-button
                    >
                </el-col>
            </el-row>
            <el-table
                :border="Global.tableShowBorder"
                v-loading="loading"
                :data="salaryBookList"
                @selection-change="handleSelectionChange"
            >
                <el-table-column
                    type="index"
                    width="50"
                    label="序号"
                    align="center"
                ></el-table-column>
                <el-table-column
                    label="日期(年月)"
                    align="center"
                    prop="periodYear
"
                    width="160"
                >
                    <template slot-scope="scope">
                        <span>{{
                            scope.row.periodYear +
                            "年" +
                            scope.row.periodMonth +
                            "月"
                        }}</span>
                    </template>
                </el-table-column>

                <el-table-column
                    label="部门"
                    align="center"
                    prop="deptName"
                    :show-overflow-tooltip="true"
                />
                <el-table-column
                    label="岗位"
                    align="center"
                    prop="postId"
                    :show-overflow-tooltip="true"
                >
                    <template slot-scope="scope">
                        <span>{{ parsePostName(scope.row.postId) }}</span>
                    </template>
                </el-table-column>

                <el-table-column
                    label="姓名"
                    align="center"
                    prop="realName"
                    width="120"
                >
                    <template slot-scope="scope">
                        <span>{{ scope.row.realName }}</span>
                    </template>
                </el-table-column>

                <el-table-column label="基本工资" align="center" prop="basic">
                    <template slot-scope="scope">
                        <span style="font-weight: 900; color: red">{{
                            util.toEsStr(scope.row.basic)
                        }}</span>
                    </template>
                </el-table-column>
                <el-table-column label="岗位工资" align="center" prop="post">
                    <template slot-scope="scope">
                        <span style="font-weight: 900; color: red">{{
                            util.toEsStr(scope.row.post)
                        }}</span>
                    </template>
                </el-table-column>
                <el-table-column
                    label="奖励工资"
                    align="center"
                    prop="position"
                >
                    <template slot-scope="scope">
                        <span style="font-weight: 900; color: red">{{
                            util.toEsStr(scope.row.position)
                        }}</span>
                    </template>
                </el-table-column>
                <el-table-column label="绩效工资" align="center" prop="house">
                    <template slot-scope="scope">
                        <span style="font-weight: 900; color: red">{{
                            util.toEsStr(scope.row.house)
                        }}</span>
                    </template>
                </el-table-column>

                <el-table-column
                    label="补贴合计"
                    align="center"
                    prop="performance"
                >
                    <template slot-scope="scope">
                        <span style="font-weight: 900; color: red">{{
                            util.toEsStr(scope.row.performance)
                        }}</span>
                    </template>
                </el-table-column>
                <el-table-column label="保险合计" align="center" prop="gdSb">
                    <template slot-scope="scope">
                        <span style="font-weight: 900; color: red">{{
                            util.toEsStr(scope.row.gdSb)
                        }}</span>
                    </template>
                </el-table-column>
                <el-table-column label="公积金合计" align="center" prop="gdGjj">
                    <template slot-scope="scope">
                        <span style="font-weight: 900; color: red">{{
                            util.toEsStr(scope.row.gdGjj)
                        }}</span>
                    </template>
                </el-table-column>

                <el-table-column
                    label="操作"
                    align="center"
                    class-name="small-padding fixed-width"
                    width="220"
                >
                    <template slot-scope="scope">
                        <el-button
                            size="mini"
                            type="text"
                            icon="el-icon-copy"
                            v-if="
                                ['0', '1', '2', '4'].includes(
                                    scope.row.staffType
                                )
                            "
                            @click="copySalary(scope.row)"
                        >
                            月复制
                        </el-button>
                        <el-button
                            size="mini"
                            type="text"
                            icon="el-icon-edit"
                            @click="adjustSalary(scope.row)"
                            v-hasPermi="['hrm:salaryBook:edit']"
                        >
                            调薪
                        </el-button>
                        <el-button
                            size="mini"
                            type="text"
                            icon="el-icon-view"
                            @click="handleView(scope.row)"
                        >
                            查看
                        </el-button>
                        <el-button
                            size="mini"
                            type="text"
                            icon="el-icon-delete"
                            @click="handleDelete(scope.row)"
                        >
                            删除
                        </el-button>
                    </template>
                </el-table-column>
            </el-table>

            <pagination
                v-show="
                    total > 10 && $store.getters.allDefectObj['HR-WEB-083'] == 1
                "
                :total="total"
                :page.sync="queryParams.pageNum"
                :limit.sync="queryParams.pageSize"
                @pagination="getList"
            />
        </TableCard>

        <!-- 添加或修改工资档案对话框 -->
        <el-dialog
            :title="title"
            :visible.sync="open"
            width="1000px"
            append-to-body
            @close="cancel"
            :close-on-click-modal="false"
        >
            <el-form
                ref="form"
                :model="form"
                :rules="readonly ? [] : rules"
                label-width="120px"
            >
                <el-col :xs="24" :sm="12" :md="12" :lg="12">
                    <el-form-item
                        label="日期"
                        :prop="form.id ? '' : 'yearMonth'"
                    >
                        <el-date-picker
                            :disabled="form.id"
                            v-model="form.yearMonth"
                            value-format="yyyy-MM"
                            type="month"
                        >
                        </el-date-picker>
                    </el-form-item>
                </el-col>
                <el-col :xs="24" :sm="12" :md="12" :lg="12">
                    <el-form-item
                        label="姓名"
                        :prop="form.id ? '' : 'realName'"
                    >
                        <el-input
                            :disabled="form.id"
                            suffix-icon="el-icon-search"
                            readonly
                            @focus="showTableDialog"
                            v-model="form.realName"
                            placeholder="请选择"
                        />
                    </el-form-item>
                </el-col>
                <el-col :xs="24" :sm="12" :md="12" :lg="12">
                    <el-form-item label="部门" :prop="form.id ? '' : 'deptId'">
                        <treeselect
                            v-model="form.deptId"
                            :options="deptOptions"
                            disabled
                            placeholder=""
                        />
                    </el-form-item>
                </el-col>
                <el-col :xs="24" :sm="12" :md="12" :lg="12">
                    <el-form-item label="岗位" :prop="form.id ? '' : 'postId'">
                        <el-select
                            disabled
                            v-model="form.postId"
                            placeholder=""
                            clearable
                        >
                            <el-option
                                v-for="item in postList"
                                :key="item.postId"
                                :label="item.postName"
                                :value="item.postId"
                            >
                            </el-option>
                        </el-select>
                    </el-form-item>
                </el-col>
                <el-col
                    :xs="24"
                    :sm="12"
                    :md="12"
                    :lg="12"
                    v-if="ifHave('basic')"
                >
                    <el-form-item
                        label="基础工资"
                        :prop="
                            $store.getters.allDefectObj['HR-WEB-084'] == 0
                                ? ''
                                : 'basic'
                        "
                    >
                        <el-input
                            :disabled="readonly"
                            type="number"
                            v-model="form.basic"
                            placeholder=""
                            @change="setNum($event, 'basic')"
                        >
                            <template slot="append">元</template>
                        </el-input>
                    </el-form-item>
                </el-col>
                <el-col
                    :xs="24"
                    :sm="12"
                    :md="12"
                    :lg="12"
                    v-if="ifHave('post')"
                >
                    <el-form-item
                        label="岗位工资"
                        prop="post"
                        :rules="[
                            {
                                required: true,
                                message: '请输入岗位工资',
                                trigger: 'blur',
                            },
                        ]"
                    >
                        <el-input
                            :disabled="readonly"
                            type="number"
                            v-model="form.post"
                            placeholder=""
                            @change="setNum($event, 'post')"
                        >
                            <template slot="append">元</template>
                        </el-input>
                    </el-form-item>
                </el-col>
                <el-col :xs="24" :sm="12" :md="12" v-if="ifHave('position')">
                    <el-form-item
                        label="奖励工资"
                        prop="position"
                        :rules="[
                            {
                                required: true,
                                message: '请输入奖励工资',
                                trigger: 'blur',
                            },
                        ]"
                    >
                        <el-input
                            :disabled="readonly"
                            type="number"
                            v-model="form.position"
                            placeholder=""
                            @change="setNum($event, 'position')"
                        >
                            <template slot="append">元</template>
                        </el-input>
                    </el-form-item>
                </el-col>
                <el-col :xs="24" :sm="12" :md="12" v-if="ifHave('performance')">
                    <el-form-item
                        label="绩效工资"
                        prop="performance"
                        :rules="[
                            {
                                required: true,
                                message: '请输入绩效工资',
                                trigger: 'blur',
                            },
                        ]"
                    >
                        <el-input
                            :disabled="readonly"
                            type="number"
                            v-model="form.performance"
                            placeholder=""
                            @change="setNum($event, 'performance')"
                        >
                            <template slot="append">元</template>
                        </el-input>
                    </el-form-item>
                </el-col>
                <el-col :xs="24" :sm="12" :md="12" v-if="ifHave('house')">
                    <el-form-item
                        label="补贴合计"
                        prop="house"
                        :rules="[
                            {
                                required: true,
                                message: '请输入补贴合计',
                                trigger: 'blur',
                            },
                        ]"
                    >
                        <el-input
                            :disabled="readonly"
                            type="number"
                            v-model="form.house"
                            placeholder=""
                            @change="setNum($event, 'house')"
                        >
                            <template slot="append">元</template>
                        </el-input>
                    </el-form-item>
                </el-col>

                <el-col :xs="24" :sm="12" :md="12" v-if="ifHave('temperature')">
                    <el-form-item label="高温补贴" prop="temperature">
                        <el-input
                            type="number"
                            v-model="form.temperature"
                            placeholder=""
                            @input="setNum"
                        >
                            <template slot="append">元</template>
                        </el-input>
                    </el-form-item>
                </el-col>
                <el-col :xs="24" :sm="12" :md="12" v-if="ifHave('traffic')">
                    <el-form-item label="交通补贴" prop="traffic">
                        <el-input
                            type="number"
                            v-model="form.traffic"
                            placeholder="请输入交通补贴"
                            @input="setNum"
                        >
                            <template slot="append">元</template>
                        </el-input>
                    </el-form-item>
                </el-col>
                <el-col :xs="24" :sm="12" :md="12" v-if="ifHave('meal')">
                    <el-form-item label="餐费补贴" prop="meal">
                        <el-input
                            type="number"
                            v-model="form.meal"
                            placeholder="请输入餐费补贴"
                            @input="setNum"
                        >
                            <template slot="append">元</template>
                        </el-input>
                    </el-form-item>
                </el-col>
                <el-col :xs="24" :sm="12" :md="12" v-if="ifHave('phone')">
                    <el-form-item label="话费补贴" prop="phone">
                        <el-input
                            type="number"
                            v-model="form.phone"
                            placeholder="请输入话费补贴"
                            @input="setNum"
                        >
                            <template slot="append">元</template>
                        </el-input>
                    </el-form-item>
                </el-col>

                <el-col
                    :xs="24"
                    :sm="24"
                    :md="24"
                    :lg="24"
                    v-if="ifTypeHava('3')"
                >
                    <el-divider content-position="left">奖金</el-divider>
                </el-col>
                <el-col :xs="24" :sm="12" :md="12" v-if="ifHave('month')">
                    <el-form-item label="月度奖金" prop="month">
                        <el-input
                            type="number"
                            v-model="form.month"
                            placeholder="请输入月度奖金"
                            @input="setNum"
                        >
                            <template slot="append">元</template>
                        </el-input>
                    </el-form-item>
                </el-col>
                <el-col :xs="24" :sm="12" :md="12" v-if="ifHave('quarter')">
                    <el-form-item label="季度奖金" prop="quarter">
                        <el-input
                            type="number"
                            v-model="form.quarter"
                            placeholder="请输入季度奖金"
                            @input="setNum"
                        >
                            <template slot="append">元</template>
                        </el-input>
                    </el-form-item>
                </el-col>
                <el-col :xs="24" :sm="12" :md="12" v-if="ifHave('year')">
                    <el-form-item label="年度奖金" prop="year">
                        <el-input
                            type="number"
                            v-model="form.year"
                            placeholder="请输入年度奖金"
                            @input="setNum"
                        >
                            <template slot="append">元</template>
                        </el-input>
                    </el-form-item>
                </el-col>
                <el-col :xs="24" :sm="12" :md="12" v-if="ifHave('attendance')">
                    <el-form-item label="全勤奖金" prop="attendance">
                        <el-input
                            type="number"
                            v-model="form.attendance"
                            placeholder="请输入全勤奖金"
                            @input="setNum"
                        >
                            <template slot="append">元</template>
                        </el-input>
                    </el-form-item>
                </el-col>
                <el-col :xs="24" :sm="12" :md="12" v-if="ifHave('recommend')">
                    <el-form-item label="内推奖金" prop="recommend">
                        <el-input
                            type="number"
                            v-model="form.recommend"
                            placeholder="请输入内推奖金"
                            @input="setNum"
                        >
                            <template slot="append">元</template>
                        </el-input>
                    </el-form-item>
                </el-col>
                <el-col
                    :xs="24"
                    :sm="24"
                    :md="24"
                    :lg="24"
                    v-if="ifTypeHava('4')"
                >
                    <el-divider content-position="left">提成工资</el-divider>
                </el-col>
                <el-col
                    :xs="24"
                    :sm="12"
                    :md="12"
                    :lg="12"
                    v-if="ifHave('sale')"
                >
                    <el-form-item label="销售提成" prop="sale">
                        <el-input
                            type="number"
                            v-model="form.sale"
                            placeholder="请输入销售提成"
                            @input="setNum"
                        >
                            <template slot="append">元</template>
                        </el-input>
                    </el-form-item>
                </el-col>
                <el-col
                    :xs="24"
                    :sm="24"
                    :md="24"
                    :lg="24"
                    v-if="ifTypeHava('5')"
                >
                    <el-divider content-position="left">计件工资</el-divider>
                </el-col>
                <el-col :xs="24" :sm="12" :md="12" v-if="ifHave('calc_count')">
                    <el-form-item label="计件工资" prop="calcCount">
                        <el-input
                            type="number"
                            v-model="form.calcCount"
                            placeholder="请输入计件工资"
                            @input="setNum"
                        >
                            <template slot="append">元</template>
                        </el-input>
                    </el-form-item>
                </el-col>
                <el-col
                    :xs="24"
                    :sm="24"
                    :md="24"
                    :lg="24"
                    v-if="ifTypeHava('6')"
                >
                    <el-divider content-position="left">计时工资</el-divider>
                </el-col>
                <el-col :xs="24" :sm="12" :md="12" v-if="ifHave('calc_time')">
                    <el-form-item label="计时工资" prop="calcTime">
                        <el-input
                            type="number"
                            v-model="form.calcTime"
                            placeholder="请输入计时工资"
                            @input="setNum"
                        >
                            <template slot="append">元</template>
                        </el-input>
                    </el-form-item>
                </el-col>
                <el-col
                    :xs="24"
                    :sm="24"
                    :md="24"
                    :lg="24"
                    v-if="ifTypeHava('7')"
                >
                    <el-divider content-position="left"
                        >工龄/司龄工资</el-divider
                    >
                </el-col>
                <el-col :xs="24" :sm="12" :md="12" v-if="ifHave('work_age')">
                    <el-form-item label="工龄工资" prop="workAge">
                        <el-input
                            type="number"
                            v-model="form.workAge"
                            placeholder="请输入工龄工资"
                            @input="setNum"
                        >
                            <template slot="append">元</template>
                        </el-input>
                    </el-form-item>
                </el-col>
                <el-col :xs="24" :sm="12" :md="12" v-if="ifHave('com_age')">
                    <el-form-item label="司龄工资" prop="comAge">
                        <el-input
                            type="number"
                            v-model="form.comAge"
                            placeholder="请输入司龄工资"
                            @input="setNum"
                        >
                            <template slot="append">元</template>
                        </el-input>
                    </el-form-item>
                </el-col>
                <el-col
                    :xs="24"
                    :sm="24"
                    :md="24"
                    :lg="24"
                    v-if="ifTypeHava('8')"
                >
                    <el-divider content-position="left">职称工资</el-divider>
                </el-col>
                <el-col
                    :xs="24"
                    :sm="12"
                    :md="12"
                    v-if="ifHave('professional')"
                >
                    <el-form-item label="职称工资" prop="professional">
                        <el-input
                            type="number"
                            v-model="form.professional"
                            placeholder="请输入职称工资"
                            @input="setNum"
                        >
                            <template slot="append">元</template>
                        </el-input>
                    </el-form-item>
                </el-col>
                <el-col :xs="24" :sm="12" :md="12" v-if="ifHave('skill')">
                    <el-form-item label="技能工资" prop="skill">
                        <el-input
                            type="number"
                            v-model="form.skill"
                            placeholder="请输入技能工资"
                            @input="setNum"
                        >
                            <template slot="append">元</template>
                        </el-input>
                    </el-form-item>
                </el-col>
                <el-col :xs="24" :sm="12" :md="12" v-if="ifHave('education')">
                    <el-form-item label="学历工资" prop="education">
                        <el-input
                            type="number"
                            v-model="form.education"
                            placeholder="请输入学历工资"
                            @input="setNum"
                        >
                            <template slot="append">元</template>
                        </el-input>
                    </el-form-item>
                </el-col>

                <el-col v-show="false" :xs="24" :sm="12" :md="12" :lg="12">
                    <el-form-item label="社保方案" prop="pgmId">
                        <el-select
                            v-model="form.pgmId"
                            placeholder="请选择社保方案"
                            clearable
                        >
                            <el-option
                                v-for="pgm in pgmList"
                                :key="pgm.id"
                                :label="pgm.name"
                                :value="pgm.id"
                            />
                        </el-select>
                    </el-form-item>
                </el-col>

                <el-col :xs="24" :sm="12" :md="12" :lg="12" v-show="false">
                    <el-form-item label="工资合计" prop="total">
                        <el-input
                            type="number"
                            v-model="form.total"
                            placeholder="请输入工资合计"
                        />
                    </el-form-item>
                </el-col>
                <el-col v-show="false" :xs="24" :sm="24" :md="24" :lg="24">
                    <el-form-item label="调薪原因" prop="adjustReason">
                        <el-input
                            v-model="form.adjustReason"
                            type="textarea"
                            :rows="5"
                            placeholder="请输入内容"
                        />
                    </el-form-item>
                </el-col>

                <el-col :xs="24" :sm="12" :md="12">
                    <el-form-item
                        label="保险合计"
                        prop="gdSb"
                        :rules="[
                            {
                                required: true,
                                message: '请输入保险合计',
                                trigger: 'blur',
                            },
                        ]"
                    >
                        <el-input
                            :disabled="readonly"
                            type="number"
                            v-model="form.gdSb"
                            placeholder=""
                            @change="setNum($event, 'gdSb')"
                        >
                            <template slot="append">元</template>
                        </el-input>
                    </el-form-item>
                </el-col>
                <el-col :xs="24" :sm="12" :md="12">
                    <el-form-item
                        label="公积金合计"
                        prop="gdGjj"
                        :rules="[
                            {
                                required: true,
                                message: '请输入公积金合计',
                                trigger: 'blur',
                            },
                        ]"
                    >
                        <el-input
                            :disabled="readonly"
                            type="number"
                            v-model="form.gdGjj"
                            placeholder=""
                            @change="setNum($event, 'gdGjj')"
                        >
                            <template slot="append">元</template>
                        </el-input>
                    </el-form-item>
                </el-col>
            </el-form>

            <div slot="footer" class="dialog-footer">
                <el-button type="primary" v-if="!readonly" @click="submitForm"
                    >保 存</el-button
                >
                <el-button v-if="!readonly" @click="cancel">取 消</el-button>
                <el-button v-if="readonly" @click="cancel">返 回</el-button>
            </div>
        </el-dialog>
        <!-- 月复制弹窗 -->
        <el-dialog
            title="月薪资基表复制"
            :visible.sync="copyOpen"
            width="600px"
            append-to-body
            @close="copyCancel"
            :close-on-click-modal="false"
        >
            <el-form
                ref="form"
                :model="form"
                :rules="rules"
                label-width="120px"
            >
                <el-col :xs="24" :sm="12" :md="12" :lg="12">
                    <el-form-item label="原日期" prop="oldYearMonth">
                        <el-date-picker
                            disabled
                            v-model="form.oldYearMonth"
                            value-format="yyyy-MM"
                            type="month"
                        >
                        </el-date-picker>
                    </el-form-item>
                </el-col>
                <el-col :xs="24" :sm="12" :md="12" :lg="12">
                    <el-form-item label="新日期" prop="yearMonth">
                        <el-date-picker
                            v-model="form.yearMonth"
                            value-format="yyyy-MM"
                            type="month"
                        >
                        </el-date-picker>
                    </el-form-item>
                </el-col>
            </el-form>

            <div slot="footer" class="dialog-footer">
                <el-button type="primary" @click="submitForm">保 存</el-button>
                <el-button @click="cancel">取 消</el-button>
            </div>
        </el-dialog>

        <userTableDialog
            title="选择员工"
            :defaultParams="defaultParams"
            :open.sync="showTableVision"
            @confirm="setFormBase"
        >
        </userTableDialog>
    </div>
</template>

<script>
import {
    listSalaryBook,
    getSalaryBook,
    delSalaryBook,
    addSalaryBook,
    updateSalaryBook,
} from "@/api/hrm/salaryBook";
import { listSocialInsncPgmAll } from "@/api/hrm/socialInsncPgm";
import { treeselect } from "@/api/system/dept";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
import { listPostAll } from "@/api/system/post";
import { listSalaryItemConfig } from "@/api/hrm/salaryItemConfig";
import moment from "moment";
export default {
    name: "SalaryBook",
    dicts: ["hrm_staff_type"],
    components: { Treeselect },
    data() {
        return {
            salaryConfigList: [],
            pgmList: [],
            // 遮罩层
            loading: true,
            // 部门树选项
            deptOptions: [],
            postList: [],
            // 选中数组
            ids: [],
            // 非单个禁用
            single: true,
            // 非多个禁用
            multiple: true,
            // 显示查询条件
            showSearch: true,
            // 总条数
            total: 0,
            // 工资档案表格数据
            salaryBookList: [],
            // 弹出层标题
            title: "",
            // 是否显示弹出层
            open: false,
            // 查询参数
            queryParams: {
                pageNum: 1,
                pageSize:
                    this.$store.getters.allDefectObj["HR-WEB-083"] == 0
                        ? 99999
                        : 10,
                deptId: null,
                realName: "",
                staffType: "",
                yearMonth: moment().subtract(1, "months").format("YYYY-MM"),
            },
            // 表单参数
            form: {},
            // 表单校验
            rules: {
                postId: {
                    required: true,
                    message: "请选择岗位",
                    trigger: "change",
                },
                deptId: {
                    required: true,
                    message: "请选择部门",
                    trigger: "change",
                },
                realName: {
                    required: true,
                    message: "请选择用户",
                    trigger: "change",
                },
                yearMonth: {
                    required: true,
                    message: "请选择日期",
                    trigger: "change",
                },
                basic: [
                    {
                        type: "number",
                        message: "请输入数字（单位元）",
                        trigger: "blur",
                        transform: (value) => {
                            value = (value + "").replace(/(^\s*)|(\s*$)/g, ""); // 先去除前后空格
                            return value || value === 0 ? Number(value) : ""; // 转为数字
                        },
                    },
                    {
                        required: true,
                        message: "基本工资不能为空",
                        trigger: "blur",
                    },
                ],
                pgmId: [
                    {
                        required: true,
                        message: "社保方案不能为空",
                        trigger: "blur",
                    },
                ],
            },

            showTableVision: false,
            showOptions: false,
            defaultParams: {
                isLink: true,
                staffTypes: "1,4,5,6",
            },
            readonly: false,
            copyOpen: false,
            copyForm: {},
        };
    },
    created() {
        this.getPgmList();
        this.getSalaryConfigList();
        this.getTreeselect();
        this.getPostData();
        this.getList();
    },
    methods: {
        copySalary(row) {
            this.reset();

            const id = row.id || this.ids;
            getSalaryBook(id).then((response) => {
                this.form = response.data;

                if (this.form.periodMonth && this.form.periodYear) {
                    this.form.oldYearMonth =
                        this.form.periodYear + "-" + this.form.periodMonth;
                }
                if (this.form.id) {
                    delete this.form.id;
                }

                this.copyOpen = true;
            });
        },
        copyCancel() {},
        copySubmit() {},
        setFormBase(row) {
            let temp = { ...row };
            console.log(temp, "temp");
            // 带入姓名 用户id  部门id 部门 所属岗位
            this.form.deptId = temp.deptId;
            this.form.userId = temp.userId;
            this.form.orgId = temp.orgId;
            this.form.staffNum = temp.staffNum;
            this.form.postId = Number(temp.postId);

            this.form.sex = temp.sex;
            this.form.staffType = temp.staffType;

            this.form.type = "0";
            this.$set(this.form, "realName", temp.realName);
        },
        showTableDialog() {
            this.showTableVision = true;
        },
        getPgmList() {
            listSocialInsncPgmAll().then((res) => {
                this.pgmList = res.data;
            });
        },
        getSalaryConfigList() {
            listSalaryItemConfig().then((res) => {
                this.salaryConfigList = res.data;
            });
        },
        /** 查询部门下拉树结构 */
        getTreeselect() {
            treeselect().then((response) => {
                this.deptOptions = response.data;
            });
        },
        getPostData() {
            listPostAll().then((res) => {
                this.postList = res.data;
            });
        },
        parsePostName(ids) {
            let nameArr = [];
            if (ids) {
                let selIds = ids.split(",");
                this.postList.forEach((item) => {
                    selIds.forEach((sel) => {
                        if (item.postId == sel) {
                            nameArr.push(item.postName);
                        }
                    });
                });
            }
            return nameArr.join(",");
        },
        /** 查询工资档案列表 */
        getList() {
            this.loading = true;

            let tempParams = this.queryParams;
            if (tempParams.yearMonth) {
                tempParams.periodYear = tempParams.yearMonth.split("-")[0];
                tempParams.periodMonth = tempParams.yearMonth.split("-")[1];
            } else {
                tempParams.periodYear = null;
                tempParams.periodMonth = null;
            }

            listSalaryBook(tempParams).then((response) => {
                this.salaryBookList = response.rows;
                this.total = response.total;
                this.loading = false;
            });
        },
        // 取消按钮
        cancel() {
            this.open = false;
            this.reset();
        },
        // 表单重置
        reset() {
            this.form = {};
            // this.form = {
            //     yearMonth: "",
            //     id: null,
            //     staffId: null,
            //     total: null,
            //     adjustReason: null,
            //     basic: null,
            //     post: null,
            //     position: null,
            //     house: null,
            //     temperature: null,
            //     traffic: null,
            //     meal: null,
            //     phone: null,
            //     performance: null,
            //     quarter: null,
            //     month: null,
            //     year: null,
            //     attendance: null,
            //     recommend: null,
            //     sale: null,
            //     calcCount: null,
            //     calcTime: null,
            //     workAge: null,
            //     comAge: null,
            //     professional: null,
            //     skill: null,
            //     education: null,
            //     createTime: null,
            //     createBy: null,
            //     updateBy: null,
            //     updateTime: null,
            // };
            this.copyOpen = false;
            this.readonly = false;
            this.resetForm("form");
        },
        /** 查询按钮操作 */
        handleQuery() {
            this.queryParams.pageNum = 1;
            this.getList();
        },
        /** 重置按钮操作 */
        resetQuery() {
            this.resetForm("queryForm");
            this.handleQuery();
        },
        // 多选框选中数据
        handleSelectionChange(selection) {
            this.ids = selection.map((item) => item.id);
            this.single = selection.length !== 1;
            this.multiple = !selection.length;
        },
        adjustSalary(row) {
            console.log(row);
            console.log(row.staffId);
            if (row.id != null) {
                this.handleUpdate(row);
            } else {
                this.handleAdd(row);
            }
        },
        /** 新增按钮操作 */
        handleAdd(row) {
            this.reset();
            this.open = true;
            this.title = "员工薪资基表创建";
            this.form.yearMonth = moment()
                .subtract(1, "months")
                .format("YYYY-MM");

            // this.form.staffId = row.staffId;
            // this.form.realName = row.realName;
            // this.form.postId = row.postId;
            // this.form.deptName = row.deptName;
            this.form.pgmId = 4;
        },
        /** 修改按钮操作 */
        handleUpdate(row) {
            this.reset();
            const id = row.id || this.ids;
            getSalaryBook(id).then((response) => {
                this.form = response.data;

                if (this.form.periodMonth && this.form.periodYear) {
                    this.form.yearMonth =
                        this.form.periodYear + "-" + this.form.periodMonth;
                }
                this.form.postId = Number(this.form.postId);

                this.open = true;
                this.title = "员工薪资基表编辑";

                this.totalToEn();
            });
        },
        //查看
        handleView(row) {
            this.reset();
            const id = row.id || this.ids;
            getSalaryBook(id).then((response) => {
                this.form = response.data;

                if (this.form.periodMonth && this.form.periodYear) {
                    this.form.yearMonth =
                        this.form.periodYear + "-" + this.form.periodMonth;
                }
                this.form.postId = Number(this.form.postId);

                this.open = true;
                this.title = "薪资详情查看";
                this.readonly = true;

                this.totalToEn();
            });
        },
        setNum(value, prop) {
            this.form[prop] = Number(value).toFixed(2);
        },
        totalToEn() {
            this.form.totalEs = this.util.toEsStr(this.form.total, true);
        },
        /** 提交按钮 */
        submitForm() {
            this.$refs["form"].validate((valid) => {
                if (this.form.yearMonth) {
                    this.form.periodYear = this.form.yearMonth.split("-")[0];
                    this.form.periodMonth = this.form.yearMonth.split("-")[1];
                }
                if (
                    this.form.basic < 0 ||
                    this.form.post < 0 ||
                    this.form.position < 0 ||
                    this.form.performance < 0 ||
                    this.form.gdSb < 0 ||
                    this.form.gdGjj < 0
                ) {
                    if (
                        this.$store.getters.allDefectObj["HR-WEB-085"] == 0 &&
                        this.form.id != null
                    ) {
                        return;
                    }
                    this.$modal.msgError("请检查输入值");
                    return false;
                }

                if (valid) {
                    if (this.form.id != null) {
                        updateSalaryBook(this.form).then((response) => {
                            this.$modal.msgSuccess("调薪成功");
                            this.open = false;
                            this.getList();
                        });
                    } else {
                        addSalaryBook(this.form).then((response) => {
                            this.$modal.msgSuccess("新增成功");
                            this.open = false;
                            this.copyOpen = false;

                            this.getList();
                        });
                    }
                }
            });
        },
        /** 删除按钮操作 */
        handleDelete(row) {
            const ids = row.id || this.ids;
            this.$confirm("请确认是否删除该记录？", "删除确认")
                .then(function () {
                    return delSalaryBook(ids);
                })
                .then(() => {
                    this.getList();
                    this.$modal.msgSuccess("删除成功");
                })
                .catch(() => {});
        },
        /** 导出按钮操作 */
        handleExport() {
            this.download(
                "hrm/salaryBook/export",
                {
                    ...this.queryParams,
                },
                `salaryBook_${new Date().getTime()}.xlsx`
            );
        },
        ifHave(code) {
            let re = false;
            this.salaryConfigList.forEach((item) => {
                if (item.code == code) {
                    if (item.status == "0") {
                        re = true;
                    }
                }
            });
            return re;
        },
        ifTypeHava(type) {
            let re = false;
            this.salaryConfigList.forEach((item) => {
                if (item.type == type) {
                    if (item.status == "0") {
                        re = true;
                    }
                }
            });
            return re;
        },
        parsePgm(pgmId) {
            let name = "";
            this.pgmList.forEach((item) => {
                if (item.id == pgmId) {
                    name = item.name;
                }
            });
            return name;
        },
    },
};
</script>
<style lang="scss" scoped>
.salaryStaffInfo {
    padding: 0px 10px;
    position: fixed;
    background-color: #ffffff;
    z-index: 9999;
    width: calc(1000px - 40px);
    justify-content: space-between;
    flex-wrap: nowrap;
    display: flex;

    span {
        font-size: 14px;
        padding: 0px 10px;
        font-weight: 900;
        color: #444444;
    }
}
</style>
